package com.pay.demo.model.base;

import com.pay.demo.dao.PayTransactionDao;
import com.pay.demo.enums.PayStatusChangeEventEnum;
import com.pay.demo.enums.PayStatusEnum;
import com.pay.demo.model.table.PayTransaction;
import com.pay.demo.tech.statemachine.PayStateMachineContext;
import com.pay.demo.tech.statemachine.PayStateMachineEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Date;
import java.util.List;

/**
 * @program: demo
 * @description: 支付流程抽象类
 * @author: guoxh
 * @create: 2019-04-01 17:39
 **/
public abstract class PayModel {

    @Autowired
    public PayTransactionDao payTransactionDao;

    @Autowired
    PayStateMachineEngine payStateMachineEngine;

    private PayTransaction payTransaction;

    //下单
    public abstract void createPayOrder();

    //发送支付短信
    public abstract void sendPaySms();

    //确认付款
    public abstract void payConfirm();

    //完成订单
    public abstract void finishPayOrder(PayStatusEnum payStatusEnum);

    /**
     * 获取多条流水记录
     *
     * @param channel
     * @return
     */
    public List<PayTransaction> getRecords(Integer channel) {
        PayTransaction payTransaction = new PayTransaction();
        payTransaction.setPayChannel(channel);
        Example<PayTransaction> example = Example.of(payTransaction);
        return payTransactionDao.findAll(example);
    }

    /**
     * 获取单条流水记录
     *
     * @param serialsNumber
     * @return
     */
    public PayTransaction getRecord(String serialsNumber) {
        PayTransaction payTransaction = new PayTransaction();
        payTransaction.setSerialsNumber(serialsNumber);
        Example<PayTransaction> example = Example.of(payTransaction);
        return payTransactionDao.findOne(example).get();
    }

    /**
     * 创建原始订单
     */
    public void createOriginOrder() {
        Date now = new Date();
        payTransaction.setCreateTime(now);
        payTransaction.setUpdateTime(now);
        PayTransaction save = payTransactionDao.save(payTransaction);
        this.payTransaction = save;
    }

    /**
     * 触发事件改变状态
     *
     * @param initialState 原始状态
     * @param trigger      事件
     * @param context      支付上下文
     */
    public void triggerEvent(PayStatusEnum initialState, PayStatusChangeEventEnum trigger, PayStateMachineContext context) {
        payStateMachineEngine.fire(initialState, trigger, context);
    }

    /**
     * 持久化订单状态
     *
     * @param payStatusEnum 订单状态
     */
    public void updateOrderStatus(PayStatusEnum payStatusEnum) {
        payTransaction.setStatus(payStatusEnum);
        payTransaction.setUpdateTime(new Date());
        PayTransaction save = payTransactionDao.save(payTransaction);
        this.payTransaction = save;
    }

    public PayTransaction getPayTransaction() {
        return payTransaction;
    }

    public void setPayTransaction(PayTransaction payTransaction) {
        this.payTransaction = payTransaction;
    }
}
